/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.seari.utils;

import com.sun.mail.util.MailSSLSocketFactory;
import java.security.GeneralSecurityException;
import java.util.Date;
import java.util.Properties;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.mail.Authenticator;
import javax.mail.Message;
import javax.mail.MessagingException;
import javax.mail.PasswordAuthentication;
import javax.mail.Session;
import javax.mail.Transport;
import javax.mail.internet.InternetAddress;
import javax.mail.internet.MimeMessage;

/**
 *
 * @author Rainbow
 */
public class MailTools
{
    private static MailTools instance = null;
    
    private static Properties prop = null;
    
    public static String mailSender = "604833898@qq.com";
    
    public static String mailReceiver = "604833898@qq.com";
    
    public static String mailToken = "kyorpwzendfrbaji";
    
    public static void sendMail(String title,String content)
    {
        Transport ts = null;
        try
        {
            //使用JavaMail发送邮件的5个步骤
            //1.txt、创建定义整个应用程序所需的环境信息的Session对象
            Session session = Session.getDefaultInstance(prop, new Authenticator() {
                @Override
                public PasswordAuthentication getPasswordAuthentication() {
                    //发件人邮件用户名、授权码
                    return new PasswordAuthentication(mailSender,mailToken);
                }
            });

            //开启Session的debug模式，这样就可以查看到程序发送Email的运行状态
            session.setDebug(true);

            //2、通过session得到transport对象
            ts = session.getTransport();

            //3、使用邮箱的用户名和授权码连上邮件服务器
            ts.connect("smtp.qq.com", mailSender, mailToken);

            //4，创建邮件
            //4-1.txt，创建邮件对象
            MimeMessage message = new MimeMessage(session);

            //4-2，指明邮件的发件人
            message.setFrom(new InternetAddress(mailSender));

            //4-3，指明邮件的收件人
            message.setRecipient(Message.RecipientType.TO, new InternetAddress(mailReceiver));

            //4-4，邮件标题
            message.setSubject(title);

            //4-5，邮件文本内容
            message.setContent(content, "text/html;charset=UTF-8");

            //4-6，发送邮件
            ts.sendMessage(message, message.getAllRecipients());

            
        } catch (Exception e)
        {
            e.printStackTrace();
        }finally{
            //5，关闭连接
            if(ts != null)
            {
                try
                {
                    ts.close();
                } catch (MessagingException ex)
                {
                    ex.printStackTrace();
                }
            }
        }
    }
    
    private MailTools()
    {
        try
        {
            prop = new Properties();
            //设置QQ邮件服务器
            prop.setProperty("mail.host", "smtp.qq.com");
            //邮件发送协议
            prop.setProperty("mail.transport.protocol", "smtp");
            //需要验证用户名密码
            prop.setProperty("mail.smtp.auth", "true");
            //关于QQ邮箱，还要设置SSL加密，加上以下代码即可
            MailSSLSocketFactory sf = new MailSSLSocketFactory();
            sf.setTrustAllHosts(true);
            prop.put("mail.smtp.ssl.enable", "true");
            prop.put("mail.smtp.ssl.socketFactory", sf);
        } catch (Exception e)
        {
            e.printStackTrace();
        }
    }
    
     public static synchronized MailTools getInstance()
     {  
        if(instance == null)
        {
            instance = new MailTools();
        }
        return instance;
     }
     
    public static void main(String[] args)
    {
        MailTools.getInstance().sendMail(DateUtil.DateToString(new Date(), DateUtil.DATE_FORMAT_YYYYMMDD2), "测试信息");
    }
}

